package com.woniu108.person.consumer;


import com.woniu108.carService.commos.util.RedisKey;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.carService.commos.util.SMSUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@RabbitListener(queues = "check_phone_queue")
public class CheckPhoneCodeConsumer {

    @Resource
    private SMSUtil smsUtil;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 消息队列：check_phone_queue,执行功能:发送验证码，将发送信息的手机号码、发送次数、有效时间存入redis
     * @param mobile
     * @throws Exception
     */
    @RabbitHandler
    public void sendMessage(String mobile) throws Exception {
        System.out.println("进入消息队列，此时取出处理的手机号码为：" + mobile);
        //调用第三方的短信服务发送验证码，注入工具类调用方法
        Integer code = (int) (Math.random() * 900000) + 100000;
        String result = smsUtil.send(mobile, code + "");
        if (result.equals("2")) {
            //发送成功
            redisUtil.setString(RedisKey.checkMobile(mobile), code + "", 5 * 60);
            redisUtil.setString(RedisKey.checkMobileOneMinute(mobile), "", 60);
            if (!redisUtil.exist(RedisKey.sendMessageNumber(mobile))) {
                redisUtil.setString(RedisKey.sendMessageNumber(mobile), "1", 60 * 60);
            } else {
                redisUtil.incr(RedisKey.sendMessageNumber(mobile));
            }
        } else {
            throw new Exception("短信发送失败");
        }
    }

}
